package com.yootk.main;

import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;

/**
 * Resource资源读取
 */
public class Demo01_Resource {
    public static void main(String[] args) throws Exception {
//        test1();
//        test2();
        test3();
    }

    /**
     * ByteArrayResource，读取内存资源
     */
    public static void test1() throws Exception {
        byte[] data = "沐言科技：www.yootk.com".getBytes();
        Resource resource = new ByteArrayResource(data);
        InputStream input = resource.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int temp = 0;
        while ((temp = input.read()) != -1) {
            if (temp >= 'a' && temp <= 'z') {
                bos.write(Character.toUpperCase(temp));
            } else {
                bos.write(temp);
            }
        }
        input.close();
        bos.close();
        System.err.println(bos);
    }

    /**
     * FileSystemResource，读取文件资源
     */
    public static void test2() throws Exception {
        String path = "F:" + File.separator + "muyan" + File.separator + "message.txt";
        Resource resource = new FileSystemResource(path);
        InputStream input = resource.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int temp = 0;
        while ((temp = input.read()) != -1) {
            if (temp >= 'a' && temp <= 'z') {
                bos.write(Character.toUpperCase(temp));
            } else {
                bos.write(temp);
            }
        }
        input.close();
        bos.close();
        System.err.println(bos);
    }

    /**
     * UrlResource，读取网络资源
     */
    public static void test3() throws Exception {
        String url = "https://www.jd.com";
        Resource resource = new UrlResource(url);
        InputStream input = resource.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int temp = 0;
        while ((temp = input.read()) != -1) {
            if (temp >= 'a' && temp <= 'z') {
                bos.write(Character.toUpperCase(temp));
            } else {
                bos.write(temp);
            }
        }
        input.close();
        bos.close();
        System.err.println(bos);
    }
}
